﻿@page "/"
@using LLAntBlazor.Web.Common
@using LLAntBlazor.Web.Model

<PageTitle>Home</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.
<span>@_visible</span>

<Button Type="ButtonType.Primary" OnClick="@(() => { _visible = true; })">点击Web</Button>

<Modal Title="对话框" @bind-Visible="_visible">

    <Line Data="BfData" Config="config"/>
    @* <span> *@
    @*     @foreach (OriginalResult o in BfData) *@
    @*     { *@
    @*         <div>@o.Time:@o.Value</div> *@
    @*     } *@
    @* </span> *@
</Modal>
<div class="external-chart-container" style="width: 100%; height: 400px">
    <Line Data="BfData" Config="config" @ref="_chart"/>
</div>


@code {

    IChartComponent _chart;

    LineConfig config = new LineConfig()
    {
        Padding = "auto",
        XField = "time",
        YField = "value",
        Smooth = true,
        
        
    };

    object[] BfData;

    protected override async Task OnInitializedAsync()
    {
        DateTime startTime = DateTime.Parse("2025-01-01 00:00:00");
        DateTime endTime = DateTime.Parse("2025-01-02 00:00:00");

        List<OriginalResult> originalResults = await Get("CO", startTime, endTime);

        BfData = originalResults.Select(s=>new
        {
            time=s.Time.ToShortTimeString(),
            value=s.Value
        }).ToArray();

        await base.OnInitializedAsync();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {

        await base.OnAfterRenderAsync(firstRender);
        await _chart.ChangeData(BfData);

    }

}

@code
{
    bool _visible = false;


    private async Task<List<OriginalResult>> Get(string name, DateTime startTime, DateTime endTime)
    {
        var helper = new BfDataHttpClient();
    // GET 请求
        var getResult = await helper.GetAsync($"http://localhost:5050/Original/{name}?startTime={startTime}&endTime={endTime}");
        if (getResult.IsSuccess)
        {
            List<OriginalResult> originalResults = getResult.GetData<List<OriginalResult>>();
            return originalResults;
        }
        return new List<OriginalResult>();

    // // POST 请求
    //     var postResult = await helper.PostAsync("https://api.example.com/create", new { Name = "John" });
    }
}